package com.basics.common.utils;

import cn.hutool.core.util.ObjectUtil;
import com.basics.common.exception.BadRequestException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**功能描述：验证工具
 * @Author Geordie
 * @ClassName ValidationUtil
 * @Date 2021/7/8
 * @Annotation
 */
public class ValidationUtil {
    /**
     * 验证空
     */
    public static void isNull(Object obj, String entity, String parameter , Object value){
        if(ObjectUtil.isNull(obj)){
            String msg = entity + " 不存在: "+ parameter +" is "+ value;
            throw new BadRequestException(msg);
        }
    }

    /**
     * 验证是否为邮箱
     */
    public static boolean isEmail(String email) {
        if (email == null)
            return false;
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(email);
        if (m.matches())
            return true;
        else
            return false;
    }
}
